import { getData } from '@vimi/utils-api'

const state = {
  creditOrg: [],
}

const getters = {
  creditOrg: state => state.creditOrg,
}

const actions = {
  getCreditOrg({ commit, rootGetters }, busType = 1) {
    return new Promise((resolve, reject) => {
      //   if (!force && state.creditOrg.length) {
      //     return resolve(state.creditOrg)
      //   }
      const params = {
        'agency_code.eq': rootGetters.org.orgCode,
        'access_status.in': '1,3',
        'bus_type.eq': busType,
      }
      getData('credit', 'credit_agency_access_bank_view', params, { access_time: 'desc' })
        .then(res => {
          const data = (res && res.data) || []
          commit('creditOrg', data)
          resolve(data)
        })
        .catch(err => {
          reject(err)
        })
    })
  },
}

const mutations = {
  creditOrg: (state, payload) => (state.creditOrg = payload),
}

export default {
  state,
  getters,
  actions,
  mutations,
}
